#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   init_driver.py
@Time    :   2024/01/21 17:51:52
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   初始化浏览器驱动
'''

from rpa_tool.browser.download_drive import download_chrome_driver
from rpa_tool.browser.get_version import get_chrome_version

import os

# 初始化浏览器驱动，返回驱动地址
def init_chrome_driver():
    driver_path = ""
    version_osname = get_chrome_version()
    name = ""
    if version_osname["os_name"] == "Linux":
        name = "linux64"
    elif version_osname["os_name"] == "Windows":
        name = "win64"
    isExist = False
    for root, dirs, files in os.walk("./chromedriver"):
        for dir in dirs:  
            if dir == version_osname["version"]:
                for rs, ds, fs in os.walk(f"./chromedriver/{dir}"):
                    for d in ds:
                        if d == f"chromedriver-{name}":
                            if name == "linux64":
                                driver_path = f"./chromedriver/{dir}/chromedriver-{name}/chromedriver"
                            elif name == "win64":
                                driver_path = f"./chromedriver/{dir}/chromedriver-{name}/chromedriver.exe"
                            isExist = True
                            break
                break
                                                    
    if not isExist:
        download_chrome_driver(f"./chromedriver/{version_osname['version']}/chromedriver.zip")
        if name == "linux64":
            driver_path = f"./chromedriver/{version_osname['version']}/chromedriver-{name}/chromedriver"
        elif name == "win64":
            driver_path = f"./chromedriver/{version_osname['version']}/chromedriver-{name}/chromedriver.exe"    

    return driver_path
